package com.springboot.news.controller;

import com.springboot.news.domain.po.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/api")
public class FileUploadController {

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws IOException {
// getOriginalFilename获取原始文件名字 transferTo 将接受的文件存入磁盘中 long getSize 获取文件大小 byte[] getBytes获取文件字节数 InputStream getInputStream获取接收到的文件内容输入流文件


        String originalName = file.getOriginalFilename();
        String fileName = UUID.randomUUID().toString() + originalName.substring(originalName.lastIndexOf("."));
        file.transferTo(new File("D:\\code\\test_path\\" + fileName));
        return Result.success("https://www.baidu.com/img/flexible/logo/pc/result.png");
    }
}
